﻿using XinLife.Application.Interface;
using XinLife.Common;
using XinLife.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using XinLife.Core.Model;
using Microsoft.AspNetCore.Hosting;
using XinLife.Application;

namespace XinLife.Controllers.Admin
{
    /// <summary>
    /// 用户管理
    /// </summary>
    public class UserController : BaseController
    {
        private IUserService _userService;

        private IWebHostEnvironment _webHostEnvironment;
        public UserController(IUserService userService, IWebHostEnvironment webHostEnvironment)
        {
            _webHostEnvironment=webHostEnvironment;
            _userService = userService;
        }

        /// <summary>
        /// 获取用户分页列表
        /// </summary>
        [HttpGet]
        public async Task<object> GetUserPageList([FromQuery] Pagination pagination, [FromQuery] string queryJson = null)
        {
            return await _userService.GetUserPageList(pagination, queryJson);
        }
        /// <summary>
        /// 获取用户
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<AjaxResult> GetUserByUserId(string userId)
        {
            return await _userService.GetUserByUserId(userId);
        }

        /// <summary>
        /// 修改用户
        /// </summary>-
        /// <returns></returns>
        [HttpPost]
        [ServiceFilter(typeof(TransactionFilter))]
        public async Task<AjaxResult> EditUser([FromForm] UserInputDto user)
        {
            return await _userService.EditUserAsync(user);
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        [HttpPost]
        public async Task<AjaxResult> DelUser([FromForm] string ids)
        {
            return await _userService.DelUserAsync(ids);
        }
        /// <summary>
        /// 重置密码
        /// </summary>
        [HttpPost]
        public async Task<AjaxResult> ResetPwd([FromForm] string id)
        {
            return await _userService.ResetPwd(id);
        }


        /// <summary>
        /// 上传文件
        /// </summary>
        [HttpPost]
        public async Task<AjaxResult> UploadFile([FromForm] IFormFile file, [FromForm] string type)
        {
            if (string.IsNullOrWhiteSpace(type))
            {
                return Error("缺少上传参数");
            }
            string path = _webHostEnvironment.WebRootPath + "/UpLoadFile/";
            path = Path.Combine(path, type);
            string newFileName = Guid.NewGuid().ToString("N") + Path.GetExtension(file.FileName);

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            int index = path.IndexOf("UpLoadFile");

            using (var stream = new FileStream(Path.Combine(path, newFileName), FileMode.Create))
            {
                await file.CopyToAsync(stream);
            }
            return Success(new { fileUrl = Path.Combine(path.Substring(index), newFileName) });
        }
    }
}
